package com.atguigu.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.*;
import com.atguigu.commonutils.R;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.atguigu.vod.service.VodService;
import com.atguigu.vod.utils.ConstantVodUtils;
import com.atguigu.vod.utils.InitVodClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/eduvod/video")
@Api(tags = "阿里云视频管理")
//@CrossOrigin
public class VodController {

    @Autowired
    private VodService vodService;

    @ApiOperation("上传视频到阿里云")
    @PostMapping("uploadAliyunVideo")
    public R uploadAliyunVideo(MultipartFile file) {
        // 上传视频返回视频的id
        String videoId = vodService.uploadVideoAly(file);
        return R.ok().data("videoId",videoId);
    }
    @ApiOperation("id删除一个阿里云视频")
    @DeleteMapping("removeAlyVideo/{id}")
    public R removeAlyVideo(@PathVariable String id) {
        try {
            DefaultAcsClient client = InitVodClient.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
            // 创建删除的request 对象
            DeleteVideoRequest request = new DeleteVideoRequest();
            // 设置删除的视频ids
            request.setVideoIds(id);
            // 调用初始化方法实现删除
            client.getAcsResponse(request);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            throw new GuliException(20001,"删除视频失败");
        }
    }
    @ApiOperation("删除多个阿里云视频")
    @DeleteMapping("deleteBatch")
    public R deleteBatch(@RequestBody List<String> videoIdList) {
        vodService.removeMoreAlyVideo(videoIdList);
        return R.ok();
    }

    @ApiOperation("根据视频id得到视频播放凭证")
    @GetMapping("getAuthor/{id}")
    public R getAuthor(@PathVariable String id) {
        try {
            DefaultAcsClient client = InitVodClient.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            request.setVideoId(id);
            GetVideoPlayAuthResponse response = client.getAcsResponse(request);
            String playAuth = response.getPlayAuth();

            GetPlayInfoRequest request1 = new GetPlayInfoRequest();
            request1.setVideoId(id);
            GetPlayInfoResponse response1 = client.getAcsResponse(request1);
            List<GetPlayInfoResponse.PlayInfo> playInfoList = response1.getPlayInfoList();
            String playURL = "";
            for (GetPlayInfoResponse.PlayInfo playInfo : playInfoList) {
                playURL = playInfo.getPlayURL();
            }
            return R.ok().data("playAuth",playAuth).data("playURL",playURL);
        }catch (Exception e) {
            throw new GuliException(20001,"获取视频播放凭证失败");
        }
    }


}
